var countRows = 1;

$(document).ready(function () {
    $('#chartAsociates').kendoChart({
        title: {
            position: 'bottom',
            text: 'Total de asociados (Universo: ' + appBosch.totalAsociates + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# ( #=kendo.toString(value/appBosch.totalAsociates,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# ( #=kendo.toString(value/appBosch.totalAsociates,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({});
        },
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.totalUsers,
                        color: '#113f8c',
                        category: "Asociados en el equipo"
                    },
                    {
                        value: (appBosch.totalAsociates - appBosch.totalUsers),
                        color: '#ccc',
                        category: "Otros asociados"
                    }
                ]
            }
        ]
    });

    $('#chartCertifications').kendoChart({
        title: {
            position: 'bottom',
            text: 'Total de certificaciones (Universo: ' + kendo.toString(appBosch.certifications.total, 'n0') + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# ( #=kendo.toString(value/appBosch.certifications.total,"p2")#)'
            }
        },
        chartArea: {
            background: ''
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# ( #=kendo.toString(value/appBosch.certifications.total,"p2")#)'
        },
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: (appBosch.certifications.total - appBosch.certifications.success - appBosch.certifications.training),
                        color: '#ccc',
                        category: "Sin certificar"
                    },
                    {
                        value: appBosch.certifications.success,
                        color: '#61ae24',
                        category: "Certificaciones"
                    },
                    {
                        value: appBosch.certifications.training,
                        color: '#ff0051',
                        category: "En entrenamiento"
                    }
                ]
            }
        ]
    });

    $('#chart90').kendoChart({
        title: {
            position: 'bottom',
            text: '3 meses (Universo: ' + appBosch.totalUsers + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            if (e.category == '>= 3 meses') {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 90
                });
            }
            else {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'lt',
                    value: 90
                });
            }
        },
        transitions: true,
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.teamUsers90,
                        color: '#e2850b',
                        category: ">= 3 meses"
                    },
                    {
                        value: (appBosch.totalUsers - appBosch.teamUsers90),
                        color: '#ccc',
                        category: "< de 3 meses"
                    }
                ]
            }
        ]
    });

    $('#chart270').kendoChart({
        title: {
            position: 'bottom',
            text: '9 meses  (Universo: ' + appBosch.totalUsers + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            if (e.category == '>= 9 meses') {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 270
                });
            }
            else {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'lt',
                    value: 270
                });
            }
        },
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.teamUsers270,
                        color: '#0f75db',
                        category: ">= 9 meses"
                    },
                    {
                        value: (appBosch.totalUsers - appBosch.teamUsers270),
                        color: '#ccc',
                        category: "< 9 meses"
                    }
                ]
            }
        ]
    });

    $('#chart450').kendoChart({
        title: {
            position: 'bottom',
            text: '15 meses (Universo: ' + appBosch.totalUsers + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            if (e.category == '>= 15 meses') {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 450
                });
            }
            else {
                appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'lt',
                    value: 450
                });
            }
        },
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.teamUsers450,
                        color: '#149728',
                        category: ">= 15 meses"
                    },
                    {
                        value: (appBosch.totalUsers - appBosch.teamUsers450),
                        color: '#ccc',
                        category: "< 15 meses"
                    }
                ]
            }
        ]
    });

    $('#chart90Without').kendoChart({
        title: {
            position: 'bottom',
            text: '3 meses sin curso inicial (Universo: ' + appBosch.teamUsers90 + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers90,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers90,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 90
                },
                {
                    field: 'level_name',
                    operator: 'eq',
                    value: 'Certificación'
                });
        },
        transitions: true,
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.withoutInicial,
                        color: '#e54028',
                        category: ">= 3 meses"
                    },
                    {
                        value: (appBosch.teamUsers90 - appBosch.withoutInicial),
                        color: '#e2850b',
                        category: "< de 3 meses"
                    }
                ]
            }
        ]
    });

    $('#chart270Without').kendoChart({
        title: {
            position: 'bottom',
            text: '9 meses sin curso intermedio (Universo: ' + appBosch.teamUsers270 + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers270,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers270,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 270
                },
                {
                    logic: 'or',
                    filters: [
                        {
                            field: 'level_name',
                            operator: 'eq',
                            value: 'Certificación'
                        },
                        {
                            field: 'level_name',
                            operator: 'eq',
                            value: 'Multihabilidad inicial'
                        }
                    ]
                });
        },
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.withoutIntermedio,
                        color: '#e54028',
                        category: ">= 3 meses"
                    },
                    {
                        value: (appBosch.teamUsers270 - appBosch.withoutIntermedio),
                        color: '#0f75db',
                        category: "< de 3 meses"
                    }
                ]
            }
        ]
    });

    $('#chart450Without').kendoChart({
        title: {
            position: 'bottom',
            text: '9 meses sin curso avanzado (Universo: ' + appBosch.teamUsers450 + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers450,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.teamUsers450,"p2")#)'
        },
        chartArea: {
            background: ''
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({
                    field: 'days',
                    operator: 'gte',
                    value: 270
                },
                {
                    logic: 'or',
                    filters: [
                        {
                            field: 'level_name',
                            operator: 'eq',
                            value: 'Certificación'
                        },
                        {
                            field: 'level_name',
                            operator: 'eq',
                            value: 'Multihabilidad inicial'
                        }
                    ]
                });
        },
        transitions: true,
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: [
                    {
                        value: appBosch.withoutAvanzado,
                        color: '#e54028',
                        category: ">= 3 meses"
                    },
                    {
                        value: (appBosch.teamUsers450 - appBosch.withoutAvanzado),
                        color: '#149728',
                        category: "< de 3 meses"
                    }
                ]
            }
        ]
    });

    $('#chartLevels').kendoChart({
        title: {
            position: 'bottom',
            text: 'Niveles (Universo: ' + appBosch.totalUsers + ')'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
            }
        },
        tooltip: {
            visible: true,
            template: '#=category#: #=value# (#=kendo.toString(value/appBosch.totalUsers,"p2")#)'
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({
                field: 'level_name',
                operator: 'eq',
                value: e.category
            });
        },
        chartArea: {
            background: ''
        },
        transitions: true,
        series: [
            {
                type: 'pie',
                startAngle: 100,
                data: appBosch.levels
            }
        ]
    });

    $('#chartCourses').kendoChart({
        title: {
            position: 'bottom',
            text: 'Cursos'
        },
        legend: {
            visible: true,
            position: 'bottom'
        },
        valueAxis: {
            labels: {
                format: "{0:p2}"
            },
            line: {
                visible: true
            },
            title: {
                text: 'Avance',
                padding: '10'
            },
            min: 0,
            max: 1
        },
        categoryAxis: {
            labels: {
                rotation: 270
            },
            title: {
                text: 'Curso',
                padding: '10'
            },
            field: "category",
            majorGridLines: {
                visible: true
            }
        },
        seriesDefaults: {
            labels: {
                visible: true,
                background: 'transparent',
                template: '#=kendo.toString(value, "p2")#'
            }
        },
        seriesClick: function (e) {
            appBosch.dataSource.filter({
                field: 'level_name',
                operator: 'eq',
                value: e.category
            });
        },
        tooltip: {
            visible: true,
            template: "#= kendo.toString(value, 'p2') #"
        },
        chartArea: {
            background: ''
        },
        transitions: true,
        series: [
            {
                type: 'column',
                data: appBosch.courses
            }
        ]
    });


    appBosch.dataSource = new kendo.data.DataSource({
        data: appBosch.teamUsers,
        batch: true,
        schema: {
            model: {
                id: "user_id",
                fields: {
                    nomina: {
                        type: 'string'
                    },
                    full_name: {
                        type: 'string'
                    },
                    curp: {
                        type: 'string'
                    },
                    admission_date: {
                        type: 'date'
                    },
                    days: {
                        type: 'number'
                    }
                }
            }
        }
    });

    $("#gridTeamUsers").kendoGrid({
        height: 500,
        toolbar: [
            {
                name: "excel"
            },
            {
                name: "pdf"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Asociados en " + appBosch.team['name'] + ".xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Asociados en " + appBosch.team['name'] + ".pdf"
        },
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: {
            mode: 'row'
        },
        selectable: true,
        navigatable: true,
        sortable: true,
        dataBound: function () {
            countRows = 1;
        },
        dataSource: appBosch.dataSource,
        columns: [
            {
                title: "#", template: '#=countRows++#', width: 70
            },
            {
                field: "nomina",
                title: "Foto",
                width: 50,
                template: '<img src="/img/workers-mini/#=nomina#.jpg"/>',
                filterable: false
            },
            {
                field: "nomina",
                title: "Nomina",
                width: 150,
                filterable: {
                    cell: {
                        operator: "contains"
                    }
                }
            },
            {
                field: "level_name",
                title: "Nivel",
                width: 150,
                template: '<i title="#=level_name#" class="fa fa-2x #=level_icon#" style="color: #=level_colour#"></i>',
                columnMenu: true,
                filterable: {
                    multi: true
                }
            },
            {
                field: "full_name",
                title: "Nombre",
                width: 300,
                filterable: {
                    cell: {
                        operator: "contains"
                    }
                }
            },
            {
                field: "curp",
                title: "CURP",
                width: 200,
                filterable: {
                    cell: {
                        operator: "contains"
                    }
                },
                hidden: true
            },
            {
                field: "admission_date",
                title: "Fecha ingreso",
                template: "<span title='#=kendo.toString(admission_date, 'D')#'>#=kendo.toString(admission_date, 'd')#</span>",
                width: 170,
                filterable: {
                    cell: {
                        operator: "eq"
                    }
                }
            },
            {
                field: "days",
                title: "Dias en la linea",
                width: 150,
                template: '#=kendo.toString(days, "n0")#',
                filterable: {
                    cell: {
                        operator: "gte"
                    }
                }
            }
        ],
        editable: false
    });
})
;

